package com.server.sercurity.vo;

import com.server.base.entity.SysRole;
import com.server.base.vo.AdminLoginSuccessVO;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author zgr
 * @date 2023/7/18 13:50
 * @description
 */
@Data
@NoArgsConstructor
public class UserDetailsVO implements UserDetails {

    private static final long serialVersionUID = 5282531710513739961L;

    private AdminLoginSuccessVO adminLoginSuccessVO;

    public UserDetailsVO(AdminLoginSuccessVO adminLoginSuccessVO) {
        this.adminLoginSuccessVO = adminLoginSuccessVO;
    }

    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>(this.adminLoginSuccessVO.getRoles().size());
        for (SysRole sysRole: this.adminLoginSuccessVO.getRoles()) {
            authorities.add(new SimpleGrantedAuthority(sysRole.getRoleName()));
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return this.adminLoginSuccessVO.getSysAdmin().getPassword();
    }

    @Override
    public String getUsername() {
        return this.adminLoginSuccessVO.getSysAdmin().getName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.adminLoginSuccessVO.getSysAdmin().getStatus();
    }
}
